<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Jhin" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="ONex B2B2C 二次开发之路由在商派的 ONex B2B2C 中，采用的路由机制是Symfony中的请求和响应组件。Symfony2使用两个类可以非常容易地与请求和响应交互，从而取代原始的PHP方式。Request类是HTTP请求面向对象的简单表示。有了它，你获取请求信息将易如反掌。
/webroot/app/base/kernel.php
12use Symfony\Component\H">
<meta property="og:type" content="article">
<meta property="og:title" content="ONex B2B2C 二次开发 路由">
<meta property="og:url" content="http://jhin.oschina.io/ShopEX/2016-10-21-05.ONex B2B2C 二次开发之路由.html">
<meta property="og:site_name" content="Jhin">
<meta property="og:description" content="ONex B2B2C 二次开发之路由在商派的 ONex B2B2C 中，采用的路由机制是Symfony中的请求和响应组件。Symfony2使用两个类可以非常容易地与请求和响应交互，从而取代原始的PHP方式。Request类是HTTP请求面向对象的简单表示。有了它，你获取请求信息将易如反掌。
/webroot/app/base/kernel.php
12use Symfony\Component\H">
<meta property="og:updated_time" content="2016-12-22T01:45:09.969Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ONex B2B2C 二次开发 路由">
<meta name="twitter:description" content="ONex B2B2C 二次开发之路由在商派的 ONex B2B2C 中，采用的路由机制是Symfony中的请求和响应组件。Symfony2使用两个类可以非常容易地与请求和响应交互，从而取代原始的PHP方式。Request类是HTTP请求面向对象的简单表示。有了它，你获取请求信息将易如反掌。
/webroot/app/base/kernel.php
12use Symfony\Component\H">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://jhin.oschina.io/ShopEX/2016-10-21-05.ONex B2B2C 二次开发之路由.html"/>


  <title> ONex B2B2C 二次开发 路由 | Jhin </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Jhin</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Jhin(ijhin@sina.com)</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-thinkphp">
          <a href="/categories/ThinkPHP-DOC" rel="section">
            
            ThinkPHP
          </a>
        </li>
      
        
        <li class="menu-item menu-item-mysql">
          <a href="/categories/MySQL" rel="section">
            
            MySQL
          </a>
        </li>
      
        
        <li class="menu-item menu-item-php">
          <a href="/categories/PHP" rel="section">
            
            PHP
          </a>
        </li>
      
        
        <li class="menu-item menu-item-linux">
          <a href="/categories/Linux" rel="section">
            
            Linux
          </a>
        </li>
      
        
        <li class="menu-item menu-item-nginx">
          <a href="/categories/Nginx" rel="section">
            
            Nginx
          </a>
        </li>
      
        
        <li class="menu-item menu-item-html">
          <a href="/categories/HTML" rel="section">
            
            HTML5
          </a>
        </li>
      
        
        <li class="menu-item menu-item-javascript">
          <a href="/categories/JavaScript" rel="section">
            
            JavaScript
          </a>
        </li>
      
        
        <li class="menu-item menu-item-onex">
          <a href="/categories/ONEX" rel="section">
            
            ONEX
          </a>
        </li>
      
        
        <li class="menu-item menu-item-yii">
          <a href="/categories/Yii2" rel="section">
            
            Yii2
          </a>
        </li>
      
        
        <li class="menu-item menu-item-shopex">
          <a href="/categories/ShopEX" rel="section">
            
            ShopEX
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                ONex B2B2C 二次开发 路由
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-10-21T14:51:55+08:00" content="2016-10-21">
              2016-10-21
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/ShopEX/" itemprop="url" rel="index">
                    <span itemprop="name">ShopEX</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="ONex-B2B2C-二次开发之路由"><a href="#ONex-B2B2C-二次开发之路由" class="headerlink" title="ONex B2B2C 二次开发之路由"></a>ONex B2B2C 二次开发之路由</h2><p>在商派的 ONex B2B2C 中，采用的路由机制是Symfony中的请求和响应组件。Symfony2使用两个类可以非常容易地与请求和响应交互，从而取代原始的PHP方式。Request类是HTTP请求面向对象的简单表示。有了它，你获取请求信息将易如反掌。</p>
<p>/webroot/app/base/kernel.php</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">use</span> <span class="title">Symfony</span>\<span class="title">Component</span>\<span class="title">HttpKernel</span>\<span class="title">Exception</span>\<span class="title">HttpException</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Symfony</span>\<span class="title">Component</span>\<span class="title">HttpKernel</span>\<span class="title">Exception</span>\<span class="title">NotFoundHttpException</span>;</div></pre></td></tr></table></figure>
<a id="more"></a>
<p>现在我们来看一下 Symfony2 对这一块的介绍：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">use</span> <span class="title">Symfony</span>\<span class="title">Component</span>\<span class="title">HttpFoundation</span>\<span class="title">Request</span>; </div><div class="line"> </div><div class="line">$request = Request::createFromGlobals(); </div><div class="line"> </div><div class="line"><span class="comment">// 请求的URI (如：/about) ，没有任何查询参数</span></div><div class="line">$request-&gt;getPathInfo(); </div><div class="line"> </div><div class="line"><span class="comment">// 分别检索GET和POST变量</span></div><div class="line">$request-&gt;query-&gt;get(<span class="string">'foo'</span>); </div><div class="line">$request-&gt;request-&gt;get(<span class="string">'bar'</span>); </div><div class="line">$request-&gt;request-&gt;get(<span class="string">'bar'</span>, <span class="string">'default value if bar does not exist'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 检索被foo标识的UploadedFile实例</span></div><div class="line">$request-&gt;files-&gt;get(<span class="string">'foo'</span>); </div><div class="line"> </div><div class="line">$request-&gt;getMethod();          <span class="comment">// GET, POST, PUT, DELETE, HEAD </span></div><div class="line">$request-&gt;getLanguages();       <span class="comment">// 客户端接受语言的数组</span></div><div class="line"></div><div class="line"><span class="comment">// 检索服务器变量</span></div><div class="line">$request-&gt;server-&gt;get(<span class="string">'HTTP_HOST'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 检索上传文件参数</span></div><div class="line">$request-&gt;files-&gt;get(<span class="string">'foo'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 检索cookie的值</span></div><div class="line">$request-&gt;cookies-&gt;get(<span class="string">'PHPSESSID'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 检索一个http头是不是规范化</span></div><div class="line">$request-&gt;headers-&gt;get(<span class="string">'host'</span>);</div><div class="line">$request-&gt;headers-&gt;get(<span class="string">'content_type'</span>);</div></pre></td></tr></table></figure>
<p>Request类在后台所做的大量工作能使你省不少心。如isSecure()方法通过用PHP检查三个不同的值来确定用户是否使用了安全链接（通过https）。</p>
<p>ParameterBags 和 Request 类的成员</p>
<p>如上，$_GET 和 $_POST 里的值可以通过公有成员query和request来访问。每一个都是 Symfony\Component\HttpFoundation\ParameterBag 类的对象，拥有以下方法： get(), has(), all() 等等。 事实上，上面例子里提到的公有成员都是ParameterBag的实例。</p>
<p>Request类还有一个公有的 attributes 成员，里面包含了PHP框架的一些运行数据。Symfony2框架在这个变量里保存了当前请求所匹配的URL路由，比如_controller，id（如果你使用了 {id} 通配符），甚至当前路由的名称（_route）。attributes成员实际就是用来保存和提供与当前请求相关的运行环境信息。</p>
<p>Symfony2还提供了一个Response类，一个HTTP响应的简单PHP表示。这允许你使用面向对象的接口去构建返回客户端所需的响应。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">use</span> <span class="title">Symfony</span>\<span class="title">Component</span>\<span class="title">HttpFoundation</span>\<span class="title">Response</span>;</div><div class="line">$response = <span class="keyword">new</span> Response();</div><div class="line"> </div><div class="line">$response-&gt;setContent(<span class="string">'&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello world!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;'</span>);</div><div class="line">$response-&gt;setStatusCode(Response::HTTP_OK);</div><div class="line">$response-&gt;headers-&gt;set(<span class="string">'Content-Type'</span>, <span class="string">'text/html'</span>);</div><div class="line"> </div><div class="line"><span class="comment">// prints the HTTP headers followed by the content</span></div><div class="line">$response-&gt;send();</div></pre></td></tr></table></figure>
<p>就算Symfony2再没提供其它工具，你也已经有了可以轻易访问请求信息的工具包和用来创建响应的面向对象接口。即使你学了更多Symfony2的功能，也请牢记，你应用程序的目标始终是解释请求，并根据你应用程序的逻辑创建相应的响应。</p>
<p>Request类和Response类都是Symfony2中名为HttpFoundation组件的一部分。该组件可以独立使用，它还提供处理会话和文件上传的类。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/ShopEX/2016-10-21-06.ONex B2B2C 二次开发之调用外部接口.html" rel="next" title="ONex B2B2C 二次开发 调用外部接口">
                <i class="fa fa-chevron-left"></i> ONex B2B2C 二次开发 调用外部接口
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/ShopEX/2016-10-21-13.ONex B2B2C 二次开发之desktop.html" rel="prev" title="ONex B2B2C 二次开发 增加desktop">
                ONex B2B2C 二次开发 增加desktop <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Jhin" />
          <p class="site-author-name" itemprop="name">Jhin</p>
          <p class="site-description motion-element" itemprop="description">Jhin 开发文档小结</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">22</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#ONex-B2B2C-二次开发之路由"><span class="nav-number">1.</span> <span class="nav-text">ONex B2B2C 二次开发之路由</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Jhin</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="/">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="/">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  



  




  
  

  

  

  

  


</body>
</html>
